package com.auxgroup.smarthome.inner.entity;

import com.alibaba.fastjson.JSONObject;
import com.auxgroup.bridge.app.inner.vo.DeviceInfoNewVo;
import com.auxgroup.smarthome.entity.AbstractGenericEntity;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Created by fju on 2017/7/25.
 * 设备信息表
 */

@Entity
@Table(name = "device_info")
public class DeviceInfoEntity extends AbstractGenericEntity{
    //主键UUID
    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(length = 36 ,nullable = false)
    private String deviceId;

    @Column(nullable = false)
    private Integer source; // 设备来源 ：古北 ,1 ：机智云， 2：其他设备

    @Column(length = 36)
    private String productKey;

    //设备Mac
    @Column(length = 36 ,nullable = false)
    private String mac;

    @Column(length = 100)
    private String did;

    @Column(length = 200)
    private String alias;

    @Column(length = 255)
    private String type;

    @Column(length = 100)
    private String deviceKey;

    @Column(length = 100)
    private String deviceLock;

    @Column(length = 255)
    private String password;

    private Integer terminalId;

    private Integer subDevice;

    @Column(length = 30)
    private String dataOne;

    @Column(length = 30)
    private String dataTwo;

    @Column(length = 30)
    private String dataThree;

    @Column(length = 20)
    private String city;

    @Column(length = 20)
    private String cityCode;

    //设备SN
    @Column(length = 64)
    private String sn;

    //(经度)位置
    @Column(length = 20)
    private String longitude;
    //(纬度)位置
    @Column(length = 20)
    private String latitude;

    //所属设备型号[逻辑外键]
    @Column(length = 36)
    private String modelId;

    //是否在线
    @Column(name="is_online", nullable = false)
    private Boolean online;

    // 最近上线时间
    @Column
    private Long latestOnlineTime = 0L;

    @Column(name="is_trash", nullable = false)
    private Boolean trash;

    public DeviceInfoEntity(String deviceId, String mac, Boolean isOnline) {
        this.deviceId = deviceId;
        this.mac = mac;
        this.online = isOnline;
        this.trash = false;
    }

    public DeviceInfoEntity() {
        this.trash = false;
    }

    public Integer getSource() {
        return source;
    }

    public void setSource(Integer source) {
        this.source = source;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getProductKey() {
        return productKey;
    }

    public void setProductKey(String productKey) {
        this.productKey = productKey;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public String getDid() {
        return did;
    }

    public void setDid(String did) {
        this.did = did;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDeviceKey() {
        return deviceKey;
    }

    public void setDeviceKey(String deviceKey) {
        this.deviceKey = deviceKey;
    }

    public String getDeviceLock() {
        return deviceLock;
    }

    public void setDeviceLock(String deviceLock) {
        this.deviceLock = deviceLock;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getTerminalId() {
        return terminalId;
    }

    public void setTerminalId(Integer terminalId) {
        this.terminalId = terminalId;
    }

    public Integer getSubDevice() {
        return subDevice;
    }

    public void setSubDevice(Integer subDevice) {
        this.subDevice = subDevice;
    }

    public String getDataOne() {
        return dataOne;
    }

    public void setDataOne(String dataOne) {
        this.dataOne = dataOne;
    }

    public String getDataTwo() {
        return dataTwo;
    }

    public void setDataTwo(String dataTwo) {
        this.dataTwo = dataTwo;
    }

    public String getDataThree() {
        return dataThree;
    }

    public void setDataThree(String dataThree) {
        this.dataThree = dataThree;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCityCode() {
        return cityCode;
    }

    public void setCityCode(String cityCode) {
        this.cityCode = cityCode;
    }

    public String getSn() {
        return sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getModelId() {
        return modelId;
    }

    public void setModelId(String modelId) {
        this.modelId = modelId;
    }

    public Boolean getOnline() {
        return online;
    }

    public void setOnline(Boolean online) {
        this.online = online;
    }

    public Boolean getTrash() {
        return trash;
    }

    public void setTrash(Boolean trash) {
        this.trash = trash;
    }

    public Long getLatestOnlineTime() {
        return latestOnlineTime;
    }

    public void setLatestOnlineTime(Long latestOnlineTime) {
        this.latestOnlineTime = latestOnlineTime;
    }

    public DeviceInfoEntity bindDidAndPasscode(String did, String passcode) {
        this.did = did;
        this.password = passcode;
        return this;
    }

    /**
     * 更新信息
     * @param sn
     * @param alias
     */
    public void updateInfo(String sn,String alias){
        if(sn != null){
            this.sn = sn;
            if(StringUtils.isNotEmpty(this.remark)) {
                JSONObject jsonObject = JSONObject.parseObject(this.remark);
                jsonObject.put("deviceSn", sn);
                this.setRemark(jsonObject.toJSONString());
            }
        }
        if(alias != null){
            this.alias = alias;
        }
    }

    public DeviceInfoNewVo createDeviceInfoNewVo(){
        DeviceInfoNewVo deviceInfoNewVo = new DeviceInfoNewVo(this.deviceId, this.source,
                this.productKey, this.mac, this.did);
        deviceInfoNewVo.setLongitude(this.longitude);
        deviceInfoNewVo.setLatitude(this.latitude);
        return deviceInfoNewVo;
    }

}
